<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends BaseController
{
    protected $model = Product::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $input = $request->input();
        $search = ['title' => '', 'start_at' => '', 'end_at' => '', 'on_status' => -1];
        $list = Product::apply($search, $input)->orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($list);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:products'
        ]);
        $id = \request("id");
        $info = Product::find($id);
        return $this->success($info);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'title' => 'required',
            'file_path' => 'required',
            'id' => 'filled|exists:products'
        ]);
        $input = $request->input();
        $id = \request("id");
        if ($id) {
            $banner = Product::where('id', $id)->first();
            $banner->update($input);
        } else {
            $info = Product::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:products'
        ]);
        $id = $request->input('id');
        Product::where('id', $id)->first()->delete();
        return $this->success([]);
    }


}

